package com.coocaa.typhoon.api.v1.userfile.provider;

import cn.hutool.core.collection.CollectionUtil;
import com.coocaa.cirrus.common.base.util.DateUtil;
import com.coocaa.cirrus.common.base.util.IdCreateUtil;
import com.coocaa.typhoon.api.third.volcengine.model.VikingDbUserFileEntity;
import com.coocaa.typhoon.api.third.volcengine.service.ImageOCRService;
import com.coocaa.typhoon.api.third.volcengine.service.VikingDbMgrService;
import com.coocaa.typhoon.api.third.volcengine.service.VikingDbUserFileService;
import com.coocaa.typhoon.api.v1.userfile.constant.UploadTypeConstant;
import com.coocaa.typhoon.api.v1.userfile.model.ExtraSaveDTO;
import com.coocaa.typhoon.api.v1.userfile.model.UploadTmpResult;
import com.coocaa.typhoon.api.v1.userfile.model.input.UploadDataModel;
import com.coocaa.typhoon.api.v1.userfile.util.FileTypeCheckUtil;
import com.coocaa.typhoon.api.v1.userfile.util.TosFinalUrlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author bijiahao
 * @date : 2025/9/19 22:22
 * @description
 */
@Service
public class UserFileAfterUploadProvider {
    @Autowired
    private VikingDbUserFileService vikingDbUserFileService;
    @Autowired
    private ImageOCRService imageOCRService;

    public void handleAfterUpload(List<UploadTmpResult> uploadTmpResultList, String userId, UploadDataModel dataModel, ExtraSaveDTO extraSaveDTO) {
        if (UploadTypeConstant.isSearchFile(dataModel.getUploadType()) || CollectionUtil.isEmpty(uploadTmpResultList)) {
            return;
        }
        List<VikingDbUserFileEntity> entityList = uploadTmpResultList.stream().map(uploadTmpResult -> getVikingDbUserFileEntity(uploadTmpResult, userId, extraSaveDTO)).collect(Collectors.toList());
        vikingDbUserFileService.upsertData(entityList);
    }

    public void handleAfterUpload(UploadTmpResult uploadTmpResult, String userId) {
        if (!FileTypeCheckUtil.isSupportVikingDb(uploadTmpResult.getFileExtName())) {
            return;
        }
        VikingDbUserFileEntity entity = getVikingDbUserFileEntity(uploadTmpResult, userId, null);
        vikingDbUserFileService.upsertData(entity);
    }

    private  VikingDbUserFileEntity getVikingDbUserFileEntity(UploadTmpResult uploadTmpResult, String userId, ExtraSaveDTO extraSaveDTO) {
        VikingDbUserFileEntity entity = new VikingDbUserFileEntity();
        entity.setUserId(userId);
        entity.setLocation(StringUtils.isBlank(uploadTmpResult.getLocation()) ? "" : uploadTmpResult.getLocation());
        entity.setShootTime(DateUtil.toTimestamp(uploadTmpResult.getShootTime(), "yyyy:MM:dd HH:mm:ss"));
        entity.setUploadTime(System.currentTimeMillis());
//        entity.setRemark(imageOCRService.ocr(uploadTmpResult.getUrl()));
        entity.setRemark(" ");
        entity.setDesc(" ");
        entity.setTosLink(uploadTmpResult.getTosLink());
//        entity.setId(IdCreateUtil.getId());
        entity.setFileType(FileTypeCheckUtil.getFileTypeForSave(uploadTmpResult.getFileExtName()));
        //TODO:
        if (Objects.equals(entity.getShootTime(), 0L)) {
            entity.setShootTime(System.currentTimeMillis());
        }
        if (null != extraSaveDTO) {
            entity.setParentFileType(StringUtils.isBlank(extraSaveDTO.getParentFileType()) ? "" : extraSaveDTO.getParentFileType().toUpperCase());
            entity.setDuration(null == extraSaveDTO.getDuration() ? 0 : extraSaveDTO.getDuration());
        }
        return entity;
    }
}
